﻿using System;
using System.Text;
using System.Timers;

namespace OptimalisationFramework.Utilities
{
    abstract class AbstractTimer
    {
        // timer
        protected System.Timers.Timer tm;
        protected TimeSpan calculationTime;
        protected DateTime d1;
        protected uint seconds;
        protected volatile bool stopProcess;

        public void SetTime(uint seconds) {
            stopProcess = false;
            this.seconds = seconds;
            tm = new System.Timers.Timer(1000 * seconds);
            tm.Elapsed += new ElapsedEventHandler(TimerStopEventHandler);
        }


        protected void StartTimer()
        {
            tm.Start();
            d1 = DateTime.Now;
        }

        protected void TimerStopEventHandler(object source, ElapsedEventArgs e)
        {
            StopTimer();
        }

        protected void StopTimer()
        {
            tm.Stop();
            calculationTime = DateTime.Now.Subtract(d1);
            stopProcess = true;                              
        }

        public string Duration()
        {
            return calculationTime.Duration().ToString();
        }

    }
}
